﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace ScopaSL.Rendering
{
    public partial class CardRenderer : UserControl
    {
        public bool IsBeingDragged, WasTouched;
        private Point mouseOffset;
        private Point previousPosition, newPosition, toNewPosition;

        public CardRenderer()
        {
            InitializeComponent();

            IsBeingDragged = false;
            WasTouched = false;

            imageCard.MouseLeftButtonUp += new MouseButtonEventHandler(card_MouseLeftButtonUp);
            imageCard.MouseLeftButtonDown += new MouseButtonEventHandler(card_MouseLeftButtonDown);
        }


        // *****************
        // INIZIALIZZAZIONE
        // *****************

        public void SetCard(WriteableBitmap wb, int column, int row)
        {


            //Finally set the Image back
            imageCard.Source = wb;



            //Canvas.SetLeft(imageCard, -((column - 1) * LayoutRoot.Width));
            //Canvas.SetTop(imageCard, -((row - 1) * LayoutRoot.Height));
        }


        // *****************
        // AGGIORNAMENTO
        // *****************

        /// <summary>
        /// Aggiorna la posizione del pezzo in base a quella del mouse nel contenitore
        /// </summary>
        /// <param name="mousePosition">La posizione del mouse relativa al contenitore</param>
        public void UpdatePosition(Point mousePosition, Size canvasSize)
        {
            newPosition.X = mousePosition.X - mouseOffset.X;
            newPosition.Y = mousePosition.Y - mouseOffset.Y;

            // Controlla se il trascinamento è appena iniziato
            if (WasTouched)
            {
                //toNewPosition.X = previousPosition.X - newPosition.X;
                //toNewPosition.Y = previousPosition.Y - newPosition.Y;
                //double distance = Math.Sqrt(Math.Pow(toNewPosition.X, 2) + Math.Pow(toNewPosition.Y, 2));

                //if (distance > 5)
                //{
                    IsBeingDragged = true;
                    WasTouched = false;
                //}
                //else
                //    IsBeingDragged = false;
            }
            else
            {
                // Nessun trascinamento appena iniziato: controlla se sto già trascinando
                if (IsBeingDragged)
                {
                    if (newPosition.X < -(this.Width / 2))
                        newPosition.X = -(this.Width / 2);
                    if (newPosition.X > canvasSize.Width - (this.Width / 2))
                        newPosition.X = canvasSize.Width - (this.Width / 2);
                    if (newPosition.Y < -(this.Height / 2))
                        newPosition.Y = -(this.Height / 2);
                    if (newPosition.Y > canvasSize.Height - (this.Height / 2))
                        newPosition.Y = canvasSize.Height - (this.Height / 2);
                    Canvas.SetLeft(this, newPosition.X);
                    Canvas.SetTop(this, newPosition.Y);

                    previousPosition = newPosition;

                    //// Nasconde il canvas overlay se sto trascinando
                    //if (canvasOverlay.Visibility == Visibility.Visible)
                    //{
                    //    canvasOverlay.Visibility = Visibility.Collapsed;
                    //    Polygon.Stroke = new SolidColorBrush(Colors.Transparent);
                    //}
                }
            }

        }

        void card_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (WasTouched)
                WasTouched = false;
            if (IsBeingDragged)
            {
                IsBeingDragged = false;
                mouseOffset = new Point(0, 0);
            }
            //if (canvasOverlay.Visibility == System.Windows.Visibility.Visible)
            //{
            //    canvasOverlay.Visibility = System.Windows.Visibility.Collapsed;
            //    Polygon.Stroke = new SolidColorBrush(Colors.Transparent);
            //}
            //else
            //{
            //    canvasOverlay.Visibility = Visibility.Visible;
            //    Polygon.Stroke = new SolidColorBrush(Color.FromArgb(180, 200, 255, 20));
            //}
        }

        void card_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            WasTouched = true;
            mouseOffset = e.GetPosition(this);
            //e.Handled = true;
        }

    }
}
